<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;

class SalesStatistics extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'command:SalesStatistics';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '销售统计';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return int
     */
    public function handle()
    {
        return 0;
    }

    public function statOldData()
    {
        $period = new \DatePeriod(
            new \DateTime('2016-11-01'),
            new \DateInterval('P1D'),
            new \DateTime('2020-03-11') //最后一天的日期不会被包含，所以要加1天
        );

        foreach ($period as $key => $value) {
            $value->format('Y-m-d');
        }


        $sql = 'select date(created_at) as date, count(sold_by_user_id) as total, sold_by_user_id, users.user_type from roll_sales left join users on roll_sales.id=users.sold_by_user_id GROUP BY date(created_at), sold_by_user_id';
        $result = DB::select($sql);
        if (empty($result)) {
            return false;
        }

        dd($result);
    }
}
